<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    :title="title"
    @ok="handleSubmit"
    @cancel="cancelSubmit"
    :cancelText="$t('business.common_cancel')"
    :okText="$t('common.sure')"
    :showCancelBtn="!disableValue"
    :showOkBtn="!disableValue"
  >
    <div>
      <Form>
        <Row class="m-b-4">
          <Col :span="4" class="!text-center">
            <span>{{ $t('business.common_currency') }}</span>
          </Col>
          <Col :span="9" class="!text-center">
            <span>
              {{
                activeKey === 'min_access'
                  ? $t('modalForm.system.system_min_deposit')
                  : t('modalForm.system.min_bet_amount')
              }}</span
            >
          </Col>
          <div class="w-8"></div>
          <Col :span="9" class="!text-center">
            <span>{{
              activeKey === 'min_access'
                ? $t('modalForm.system.system_min_withdrawal')
                : $t('modalForm.system.max_bet_amount')
            }}</span>
          </Col>
        </Row>
        <FormItem v-for="(item, index) in sortList(getList)" :key="index">
          <Row>
            <Col :span="4">
              <div class="p-t-2">
                <cdIconCurrency class="!w-5" :icon="item.label" />
                <span class="!align-middle m-l-2">{{ item.label }}</span>
              </div>
            </Col>
            <Col :span="9">
              <InputNumber
                :disabled="disableValue"
                :size="FORM_SIZE"
                :placeholder="
                  activeKey === 'min_access'
                    ? t('modalForm.system.system_min_deposit')
                    : t('modalForm.system.max_bet_tip')
                "
                v-model:value="item.value[0]"
                min="0"
                :stringMode="true"
              />
            </Col>
            <div class="w-8 !text-center flex justify-center items-center"> </div>
            <Col :span="9">
              <InputNumber
                :disabled="disableValue"
                :size="FORM_SIZE"
                :placeholder="
                  activeKey === 'min_access'
                    ? t('modalForm.system.system_min_withdrawal')
                    : t('modalForm.system.max_bet_amount_tip')
                "
                v-model:value="item.value[1]"
                :stringMode="true"
                min="0"
              />
            </Col>
          </Row>
        </FormItem>
        <Loading :loading="loading" :absolute="false" />
      </Form>
    </div>
  </BasicModal>
</template>
<script lang="ts" setup name="AccessMoneySettingModalbet">
  import { ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form';
  import { getSchemaBase, schemaTable } from './accessMoneySettingModal.data';
  import { useFormSetting } from '/@/hooks/setting/useFormSetting';
  import { useTreeListStore } from '/@/store/modules/treeList';
  import {
    getSiteBrandDetail,
    updateSiteBrand,
    getBrandDetail,
    updateMinigameBrand,
  } from '/@/api/sys';
  import { message, InputNumber, Form, FormItem, Row, Col } from 'ant-design-vue';
  import { Loading } from '/@/components/Loading';
  import cdIconCurrency from '/@/components-cd/Icon/currency/cd-icon-currency.vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { sortList } from '/@/utils/common.ts';

  const { currencyTreeList } = useTreeListStore();
  const FORM_SIZE = useFormSetting().getFormSize;
  const activeKey = ref('min_access');
  const getList = ref([] as any);
  const loading = ref(false as any);
  const emit = defineEmits(['update:ok', 'reloadUpdate']);
  const title = ref('' as string);
  const disableValue = ref(false as any);
  const { t } = useI18n();
  const props = defineProps({
    getInfoLimit: {
      type: Object,
      default: () => ({}),
    },
  });

  // const [registerForm, { resetFields, setFieldsValue, validate, clearValidate, getFieldsValue }] = useForm({
  //   labelWidth: 100,
  //   baseColProps: { span: 22 },
  //   showActionButtonGroup: false,
  //   schemas: schemaTable,
  //   actionColOptions: {
  //     span: 22,
  //   },
  //   size: FORM_SIZE,
  // });

  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    await setModalProps({ confirmLoading: false });
    loading.value = true;
    const { type } = data;
    activeKey.value = type;
    if (type === 'bet_limit') {
      const { data, status } = await getBrandDetail({ tag: 'mini_game_limit' });
      if (status) {
        loading.value = false;
        disableValue.value = true;
        title.value = t('modalForm.system.bet_limit_amount'); //投注限额（仅对自研游戏限制）
        const newResult = {};
        for (const key in data) {
          const newKey = key.substring(1);
          newResult[newKey] = data[key];
        }
        getList.value = sortById(
          currencyTreeList.map((item) => {
            let itemData = newResult[item.id];
            if (itemData) {
              return {
                id: item.id,
                label: item.name,
                value: itemData ? itemData : ['1', '100'],
              };
            } else {
              return {
                id: item.id,
                label: item.name,
                value: ['1', '100'],
              };
            }
          }),
        );
      }
    } else {
      const { data, status } = await getBrandDetail({ tag: 'amount' });
      if (status) {
        disableValue.value = false;
        title.value = t('modalForm.system.system_settings_deposit');
        loading.value = false;
        const restructuredData = {};
        for (const key in data) {
          const newKey = key.substring(1);
          restructuredData[newKey] = data[key];
        }
        getList.value = sortById(
          currencyTreeList.map((item) => {
            let itemData = restructuredData[item.id];
            if (itemData) {
              return {
                id: item.id,
                label: item.name,
                value: [itemData.d || '1', itemData.w || '100'],
              };
            } else {
              return {
                id: item.id,
                label: item.name,
                value: ['1', '100'],
              };
            }
          }),
        );
      }
    }
  });
  function sortById(array) {
    return array.sort((a, b) => {
      return parseInt(a.id) - parseInt(b.id);
    });
  }
  async function handleSubmit() {
    try {
      const result = {};
      if (activeKey.value === 'bet_limit') {
        //const result = {};
        getList.value.forEach((item) => {
          const key = 'c' + item.id;
          result[key] = item.value;
        });

        for (const key in result) {
          if (result.hasOwnProperty(key)) {
            if (result[key]['d'] == null) {
              return message.error(t('modalForm.system.min_bet_amount_tip')); //
            }
            if (result[key]['w'] == null) {
              return message.error(t('modalForm.system.max_bet_amount_tip0')); //请输入最高投注
            }
            if (Number(result[key]['d']) > Number(result[key]['w'])) {
              return message.error(t('modalForm.system.min_max_bet_tip')); //最低投注不能大于最高投注
            }
          }
        }
      } else {
        //const result = {};
        getList.value.forEach((item) => {
          const key = 'c' + item.id;
          result[key] = {
            d: item.value[0],
            w: item.value[1],
          };
        });
        for (const key in result) {
          if (result.hasOwnProperty(key)) {
            if (result[key]['d'] == null) {
              return message.error('请输入最低存款');
            }
            if (result[key]['w'] == null) {
              return message.error('请输入最低取款');
            }
            if (Number(result[key][0]) > Number(result[key][1])) {
              return message.error('最低存款不能大于最低取款');
            }
          }
        }
      }
      const name = activeKey.value === 'bet_limit' ? 'minigame' : 'amount';
      const { data, status } = await updateSiteBrand({
        content: JSON.stringify(result),
        name: name,
      });
      if (status) {
        message.success(data);
        closeModal();
      } else {
        message.error(data);
      }
      //emit('sendParams', result,activeKey.value);
      //closeModal();
    } catch (e) {
      console.log(e);
    }
  }

  function cancelSubmit() {
    emit('reloadUpdate');
  }
</script>
